package com.maidou.qiantai.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.maidou.qiantai.common.utils.util.WebUtils;
import com.maidou.qiantai.domain.Spu;
import com.maidou.qiantai.vo.spu.SpuRecommendVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maidou.qiantai.mapper.OsRecordMapper;
import com.maidou.qiantai.domain.OsRecord;
import com.maidou.qiantai.service.IOsRecordService;

import javax.annotation.Resource;

/**
 * 用户浏览记录Service业务层处理
 * 
 * @author maidou
 * @date 2024-03-14
 */
@Service
public class OsRecordServiceImpl implements IOsRecordService 
{
    @Resource
    private OsRecordMapper osRecordMapper;

    /**
     * 查询用户浏览记录
     * 
     * @param id 用户浏览记录主键
     * @return 用户浏览记录
     */
    @Override
    public OsRecord selectOsRecordById(Long id)
    {
        return osRecordMapper.selectOsRecordById(id);
    }

    /**
     * 查询用户浏览记录列表
     * 
     * @param osRecord 用户浏览记录
     * @return 用户浏览记录
     */
    @Override
    public List<OsRecord> selectOsRecordList(OsRecord osRecord)
    {
        return osRecordMapper.selectOsRecordList(osRecord);
    }

    /**
     * 新增用户浏览记录
     * 
     * @param osRecord 用户浏览记录
     * @return 结果
     */
    @Override
    public int insertOsRecord(OsRecord osRecord)
    {
        osRecord.setCreateTime(DateUtils.getNowDate());
        return osRecordMapper.insertOsRecord(osRecord);
    }

    /**
     * 修改用户浏览记录
     * 
     * @param osRecord 用户浏览记录
     * @return 结果
     */
    @Override
    public int updateOsRecord(OsRecord osRecord)
    {
        osRecord.setUpdateTime(DateUtils.getNowDate());
        return osRecordMapper.updateOsRecord(osRecord);
    }

    /**
     * 批量删除用户浏览记录
     * 
     * @param ids 需要删除的用户浏览记录主键
     * @return 结果
     */
    @Override
    public int deleteOsRecordByIds(Long[] ids)
    {
        return osRecordMapper.deleteOsRecordByIds(ids);
    }

    /**
     * 删除用户浏览记录信息
     * 
     * @param id 用户浏览记录主键
     * @return 结果
     */
    @Override
    public int deleteOsRecordById(Long id)
    {
        return osRecordMapper.deleteOsRecordById(id);
    }

    /**
     * 获取用户浏览记录
     * @return
     */
    @Override
    public List<SpuRecommendVo> selectRecordList() {
        Long userId = WebUtils.getMemberIdByAccessToken();
        List<Spu>  list = osRecordMapper.selectSpuRecordListByUserId(userId);
        List<SpuRecommendVo> collect = list.stream().map(item -> {
            SpuRecommendVo spuRecommendVo = new SpuRecommendVo();
            BeanUtils.copyProperties(item, spuRecommendVo);
            return spuRecommendVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void updateOsRecordBySpuIdAndMemberId(OsRecord osRecord) {
        osRecordMapper.updateOsRecordBySpuIdAndMemberId(osRecord);
    }
}
